package aes.gui;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import aes.general.TeacherControl;

import com.lowagie.text.Font;

/**
 * This panel allows the teacher to change student's grade manually
 * after the student has completed the exam and after the system has
 * checked it. Teacher also must provide an explanation to the change.   
 */
public class TeacherChangeGradePanel extends JPanel {
	
	/**
	 *    
	 */
	private static final long serialVersionUID = 1L;
	private JPanel jp;
	private JLabel l1;
	private JSeparator separator;
	private JTextField sidtext;
	private JTextField examtext;
	private JTextField oldgtext;
	private JTextField newgrtext;
	private JTextArea dsctext;
	
	private ArrayList<String> gradeinfo;

	public TeacherChangeGradePanel(JPanel jp,ArrayList<String> info) {
		super();
		this.jp = jp;
		gradeinfo = info;
		initialize();
		
	}

	private void initialize() {
		setLayout(null);
		separator = new JSeparator();
		separator.setBounds(10, 43, 620, 2);
		add(separator);
		
		l1 = new JLabel("Change Grade");
		l1.setFont(new java.awt.Font("Tempus Sans ITC", java.awt.Font.BOLD, 21));
		l1.setBounds(235, 11, 169, 29);
		add(l1);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
		CardLayout cl = (CardLayout)(jp.getLayout());
     			cl.show(jp,"approve");
			}
		});
		btnCancel.setBounds(334, 395, 89, 23);
		add(btnCancel);
		
		JButton btnChange = new JButton("Change");
		btnChange.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(changeGrade()){
				JOptionPane.showMessageDialog(null, "Grade Changed",
						"OK", JOptionPane.INFORMATION_MESSAGE);
				CardLayout cl = (CardLayout)(jp.getLayout());
         			cl.show(jp,"approve");
				}
			}
		});
		btnChange.setBounds(175, 395, 89, 23);
		add(btnChange);
		
		JLabel lblStuden = new JLabel("Studen Id");
		lblStuden.setBounds(293, 80, 71, 14);
		add(lblStuden);
		
		sidtext = new JTextField();
		sidtext.setFont(new java.awt.Font("Tahoma", Font.BOLD, 14));
		sidtext.setBounds(396, 77, 86, 20);
		sidtext.setEditable(false);

		add(sidtext);
		sidtext.setColumns(10);
		sidtext.setText(gradeinfo.get(1));
		
		JLabel lblExamId = new JLabel("Exam Id ");
		lblExamId.setBounds(48, 80, 60, 14);
		add(lblExamId);
		
		examtext = new JTextField();
		examtext.setEditable(false);
		examtext.setFont(new java.awt.Font("Tahoma", Font.BOLD, 14));
		examtext.setBounds(140, 77, 86, 20);
		add(examtext);
		examtext.setColumns(10);
		examtext.setText(gradeinfo.get(0));
		JLabel lblOldGrade = new JLabel("Old Grade");
		lblOldGrade.setBounds(48, 146, 60, 14);
		add(lblOldGrade);
		
		oldgtext = new JTextField();
		oldgtext.setFont(new java.awt.Font("Tahoma", Font.BOLD, 14));
		oldgtext.setEditable(false);
		oldgtext.setBounds(140, 143, 86, 20);
		add(oldgtext);
		oldgtext.setColumns(10);
		oldgtext.setText(gradeinfo.get(2));
		
		newgrtext = new JTextField();
		newgrtext.setFont(new java.awt.Font("Tahoma",Font.BOLD, 14));
		newgrtext.setBounds(396, 143, 86, 20);
		add(newgrtext);
		newgrtext.setColumns(10);
		
		JLabel lblNewGrade = new JLabel("New Grade");
		lblNewGrade.setBounds(293, 146, 71, 14);
		add(lblNewGrade);
		
		JLabel lblDescription = new JLabel("Description ");
		lblDescription.setBounds(48, 204, 71, 14);
		add(lblDescription);
		
		dsctext = new JTextArea();
		dsctext.setBounds(48, 233, 516, 114);
		dsctext.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		add(dsctext);
		
	}

	/**
	 * Change grade.
	 * 
	 * @return true, if successful
	 */
	protected boolean changeGrade() {
		
		if (newgrtext.getText().trim().length() == 0) {
			JOptionPane.showMessageDialog(null, "Error,new grade field is Empty", "Error",
					JOptionPane.ERROR_MESSAGE);
		    return false;
	    }
		else
			if(Integer.parseInt(newgrtext.getText()) > 100 || Integer.parseInt(newgrtext.getText()) < 0){
				JOptionPane.showMessageDialog(null, "Error,Invalid Grade", "Error",
						JOptionPane.ERROR_MESSAGE);
			    return false;
			}
		try {
			Integer.decode(newgrtext.getText());
		} catch (Exception e) {
			JOptionPane.showMessageDialog(null, "Error,Grade must be a Number",
					"Error", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		
		gradeinfo.add(newgrtext.getText());
		if (dsctext.getText().trim().length() == 0) {
			JOptionPane.showMessageDialog(null, "Error,Description field is Empty", "Error",
					JOptionPane.ERROR_MESSAGE);
		    return false;
		}
		gradeinfo.add(dsctext.getText());
		TeacherControl.SetNewGrade(gradeinfo);
		return true;
	}
}
